//
//  TalkDetailViewController.m
//  SuperNews
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "TalkDetailViewController.h"
#import "TalkModel.h"
#import "CommentModel.h"
#import "AnswerModel.h"
#import "HeaderImageCell.h"
#import "HeaderLabelCell.h"
#import "QuesTableViewCell.h"
#import "DetailHeaderLabelCell.h"

@interface TalkDetailViewController () <UITableViewDelegate, UITableViewDataSource, HeaderLabelCellDelegate, DetailHeaderLabelCellDelegate>

@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) UILabel *labelDetail;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) NSMutableArray *quesArr;
@property (nonatomic, strong) NSMutableArray *answerArr;
@property (nonatomic, assign) BOOL ret;

@end

@implementation TalkDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.ret = 0;
    self.quesArr = [NSMutableArray array];
    self.answerArr = [NSMutableArray array];
    self.number = 0;
    [self createViews];
    [self getData];
}

- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/hotqa/%@/%ld-10.html", self.talkModel.expertId, self.number];
    [AFNetTool getNetWithURL:str body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *arr = [responseObject objectForKey:@"data"];
        for (NSDictionary *tempDic in arr) {
            CommentModel *comModel = [[CommentModel alloc] init];
            [comModel setValuesForKeysWithDictionary:[tempDic objectForKey:@"question"]];
            [self.quesArr addObject:comModel];
            AnswerModel *ansModel = [[AnswerModel alloc] init];
            [ansModel setValuesForKeysWithDictionary:[tempDic objectForKey:@"answer"]];
            [self.answerArr addObject:ansModel];
        }
        [self.tableV reloadData];
        self.number += 10;
        [self.tableV.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

- (void)createViews
{
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil];
    
    self.navigationItem.title = self.talkModel.alias;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 54) style:UITableViewStyleGrouped];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
    
    [self.tableV registerClass:[HeaderImageCell class] forCellReuseIdentifier:@"cell"];
    [self.tableV registerClass:[HeaderLabelCell class] forCellReuseIdentifier:@"cellLabel"];
    [self.tableV registerClass:[QuesTableViewCell class] forCellReuseIdentifier:@"cellQues"];
    [self.tableV registerClass:[DetailHeaderLabelCell class] forCellReuseIdentifier:@"cellDetail"];
    self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    }];
    self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
//    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
//    [imageV sd_setImageWithURL:(NSURL *)self.talkModel.picurl placeholderImage:[UIImage imageNamed:@"placehodler"]];
//    [self.view addSubview:imageV];
//    
//    self.labelDetail = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, <#CGFloat width#>, <#CGFloat height#>)]
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.quesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        if (0 == indexPath.row) {
            HeaderImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.imageV sd_setImageWithURL:(NSURL *)self.talkModel.picurl placeholderImage:[UIImage imageNamed:@"placehodler"]];
            cell.labelTitle.text = self.talkModel.alias;
            cell.labelNumber.text = [NSString stringWithFormat:@"——%@关注——", self.talkModel.concernCount];
            return cell;
        } else {
            if (0 == self.ret) {
                HeaderLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellLabel"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.delegate = self;
                cell.label.text = self.talkModel.mDescription;
                [cell.imageHeader sd_setImageWithURL:[NSURL URLWithString:self.talkModel.headpicurl]];
                cell.nameLabel.text = self.talkModel.name;
                cell.titleLabel.text = self.talkModel.title;
                return cell;
            } else {
                DetailHeaderLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellDetail"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.label.text = self.talkModel.mDescription;
                cell.delegate = self;
                [cell.imageHeader sd_setImageWithURL:[NSURL URLWithString:self.talkModel.headpicurl]];
                cell.nameLabel.text = self.talkModel.name;
                cell.titleLabel.text = self.talkModel.title;
                return cell;
            }
        }
    } else {
        if (0 == indexPath.row) {
            CommentModel *comModel = [self.quesArr objectAtIndex:indexPath.section];
            QuesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellQues"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.imageHeader sd_setImageWithURL:(NSURL *)comModel.userHeadPicUrl];
            cell.userNameLabel.text = comModel.userName;
            cell.commentLabel.text = comModel.content;
            return cell;
        } else {
            AnswerModel *ansModel = [self.answerArr objectAtIndex:indexPath.section];
            QuesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellQues"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.imageHeader sd_setImageWithURL:(NSURL *)ansModel.specialistHeadPicUrl];
            cell.userNameLabel.text = ansModel.specialistName;
            cell.commentLabel.text = ansModel.content;
            return cell;
        }
        
    }
}

- (void)buttonClickHandle
{
    self.ret = !self.ret;
    [self.tableV reloadData];
}

- (void)handleClick
{
    self.ret = !self.ret;
    [self.tableV reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        if (0 == indexPath.row) {
            return [UIScreen mainScreen].bounds.size.height / 4.0;
        } else {
            if (0 == self.ret) {
                return 145;
            } else {
                return 205;
            }
        }
    } else {
        return 110;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
